#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMap>
#include <QFile>
#include <QTextStream>
#include <QThread>
#include <QMessageBox>
#include <QFileDialog>
#include <QStringConverter>

// 写入文件的工作类（放在 mainwindow.cpp 或单独 .h/.cpp 文件中）
class FileWriterWorker : public QObject {
    Q_OBJECT
public:
    explicit FileWriterWorker(const QString &filePath) : filePath(filePath) {}

public slots:
    void writeFile() {
        QFile file(filePath);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            emit error("无法打开文件！");
            return;
        }

        QTextStream out(&file);
        out.setEncoding(QStringConverter::Utf8);

        for (int i = 0; i < 100; ++i) {
            out << "“你好”，“文件”\n";
            out.flush();  // 马上写入
            QThread::msleep(500);  // 等待 500ms
        }
        file.close();
        emit finished();  // 写完发出信号
    }

signals:
    void finished();
    void error(const QString &msg);

private:
    QString filePath;
};


class QTableWidget;
class QPushButton;
class QAction;

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private slots:
    void  handleMyButtonClick();   // 打开新页的槽函数
    void toggleTableOrder();
    void startFileWriting();


private:
    Ui::MainWindow *ui;
    int pageCount = 0;  // 页面计数器
    QMap<QAction*, QWidget*> windowsMap;

    QTableWidget *tableWidget = nullptr;
    QPushButton *toggleButton = nullptr;
    QPushButton *pushtxtButton = nullptr;
    bool isReversed = false; // 当前是否倒序显示

};



#endif // MAINWINDOW_H
